package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.base.UserContextInfo;
import com.mars.common.constant.Constant;
import com.mars.common.response.PageInfo;
import com.mars.common.util.RequestUtils;
import com.mars.common.util.TokenUtils;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysCatNurture;
import com.mars.module.admin.entity.SysUserCollection;
import com.mars.module.admin.mapper.SysUserCollectionMapper;
import com.mars.module.admin.service.ISysDictDataService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysCatRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysCatMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysCat;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysCatService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 猫咪业务层处理
 *
 * @author mars
 * @date 2024-03-29
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysCatServiceImpl implements ISysCatService {

    private final SysCatMapper baseMapper;
    private final ISysDictDataService dataService;
    private final SysUserCollectionMapper collectionMapper;
    private final TokenUtils tokenUtils;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysCat add(SysCatRequest request) {
        SysCat entity = SysCat.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysCatRequest request) {
        SysCat entity = SysCat.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysCat getById(Long id) {
        SysCat sysCat = baseMapper.selectById(id);
        HttpServletRequest request = RequestUtils.getRequest();
        Long userId = tokenUtils.getUserId(request);
        if (userId != null) {
            if (collectionMapper.selectCount(new QueryWrapper<SysUserCollection>().eq("user_id", userId).eq("cat_id", id)) > 0) {
                sysCat.setIsCollect("1");
            } else {
                sysCat.setIsCollect("0");
            }
        }
        return sysCat;
    }

    @Override
    public PageInfo<SysCat> pageList(SysCatRequest request) {
        Page<SysCat> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysCat> query = this.buildWrapper(request);
        IPage<SysCat> pageRecord = baseMapper.selectPage(page, query);
        List<SysCat> records = pageRecord.getRecords();
        List<SysCat> recordsNew = new ArrayList<>();
        for (SysCat cat : records) {
            cat.setSex(dataService.getDictDataById("sex", cat.getSex()));
            recordsNew.add(cat);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysCat> list(SysCatRequest request) {
        LambdaQueryWrapper<SysCat> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysCat> buildWrapper(SysCatRequest param) {
        LambdaQueryWrapper<SysCat> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getName())) {
            query.like(SysCat::getName, param.getName());
        }
        if (StringUtils.isNotBlank(param.getSex())) {
            query.like(SysCat::getSex, param.getSex());
        }
        if (StringUtils.isNotBlank(param.getType())) {
            query.like(SysCat::getType, param.getType());
        }
        if (StringUtils.isNotBlank(param.getSterilization())) {
            query.like(SysCat::getSterilization, param.getSterilization());
        }
        if (StringUtils.isNotBlank(param.getUrl())) {
            query.like(SysCat::getUrl, param.getUrl());
        }
        return query;
    }

}
